<?php

class AdminModule extends WebModule {
	
	const DEFAULT_ROUTE = '/admin/default/';
	const OPERATOR_ADMIN = '/admin/operator/';
	const PROVIDER_ADMIN = '/admin/provider/';
	const DEALER_ADMIN = '/admin/dealer/';
	const AGENT_ADMIN = '/admin/agent/';
	const COMMON_VIEW = 'application.modules.admin.views.common.';
	const COMMON_LAYOUT = 'application.modules.admin.views.layouts.';
	const COMS = 'application.modules.admin.components';
	const ACTIONS = 'application.modules.admin.actions.';
	const VIEWS = 'application.modules.admin.views.';
	
	public $defaultController = 'default/login';
	
	private static $_config = null;
	
	public static function getConfig() {
		if(!self::$_config) {
			self::$_config = Yii::createComponent(array(
				'class' => 'lib.helpers.CConfigReader',
				'baseAlias' => 'admin.config',
			));
		}
		return self::$_config;
	}
	
	protected function init() {
		parent::init();
		$this->setImport(array(
			$this->name . '.components.ui.*',
		));
	}
	
	/**
	 * @return string the base URL that contains all published asset files of gii.
	 */
	public static function getAssetsUrl()
	{
		if(self::$_assetsUrl===null)
			self::$_assetsUrl=Yii::app()
				->getAssetManager()
				->publish(dirname(__FILE__).'/assets', false, -1, true);
		return self::$_assetsUrl;
	}
	
	public function beforeControllerAction($controller, $action) {
		if(Yii::app()->adminUser->isGuest) {
			if(strpos('/'.Yii::app()->getRequest()->getPathInfo(), self::DEFAULT_ROUTE.'login') === false) {
				$controller->redirect(
					$controller->createUrl(Yii::app()->adminUser->getLoginUrl()
				));
			}
		} else if (Yii::app()->adminUser->role == AdminUser::ROLE_MEMBER) {
			// 前台用户
			$controller->redirect(Yii::app()->createUrl('/'));
		} else if (!empty(Yii::app()->adminUser->groupId)) {
			// 工作人员
			Purview::operate($controller->route);
		}
		return true;
	}
	
}

?>